package helpers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import models.AutoSiteModel;
import models.NewsModel;

public class CrawlerHelper {
	
	public static ArrayList<String> getLastestNews(AutoSiteModel autoSite) {
		ArrayList<String> listNews = new ArrayList<String>();
		try {
			Document doc = Jsoup.connect(autoSite.getUrl()).get();
			Elements links = doc.select(autoSite.getRegLink());
			if (links.size() > 0) {
				for (int i = 0; i < links.size(); i++) {
					if(!Pattern.matches("^http://[\\w\\W]+$",links.get(i).attr("href")))
					{
						listNews.add("http://"+UrlHelper.fomatDomains(autoSite.getUrl()) + links.get(i).attr("href"));
					} else {
						listNews.add(links.get(i).attr("href"));
					}
				}
			}
			
		} catch (Exception e) {
			listNews = null;
			e.printStackTrace();
		}
		return listNews;
	}	
	
	public static ArrayList<String> getPageNews(AutoSiteModel autoSite) {
		ArrayList<String> listNews = new ArrayList<String>();
		try {
			Document doc = Jsoup.connect(autoSite.getSitePage() + autoSite.getCurrentPage()).get();
			Elements links = doc.select(autoSite.getRegLink());
			if (links.size() > 0) {
				for (int i = 0; i < links.size(); i++) {
					if(!Pattern.matches("^http://[\\w\\W]+$",links.get(i).attr("href")))
					{
						listNews.add("http://"+UrlHelper.fomatDomains(autoSite.getUrl()) + links.get(i).attr("href"));
					} else {
						listNews.add(links.get(i).attr("href"));
					}	
				}
			}
			
		} catch (Exception e) {
			listNews = null;
			e.printStackTrace();
		}
		
		return listNews;
	}
	
	public static NewsModel getNews(String url, AutoSiteModel autoSite) {
		NewsModel news = new NewsModel();
		try {
			Document doc = Jsoup.connect(url).get();
			String title = doc.select(autoSite.getRegTitle()).html();
			if (!autoSite.getRegRemove().isEmpty()) {
				doc.select(autoSite.getRegRemove()).remove();
			}
			String description = doc.select(autoSite.getRegContent()).html();
			//Get image storage google and read image
			Elements imgs = doc.select(autoSite.getRegContent()).select("img");
			for (int i = 0; i < imgs.size(); i++) {
				
				String src = imgs.get(i).attr("src");
				if(!Pattern.matches("^(http|https)://[\\w\\W]+$",src))
				{
					//src = "http://"+UrlHelper.fomatDomains(autoSite.getUrl()) + src;
					description = description.replace(src, "http://"+UrlHelper.fomatDomains(autoSite.getUrl()) + src);
				}
				
				/*String name = UrlHelper.getMainDomain(src);
				GoogleStorageHelper.getFile(src, name);
				description = description.replace(src, "http://static.services79.com/ats100/" + name);*/
			}
			
			List<String> labels = new ArrayList<String>();
			labels.add(autoSite.getCategory());
			
			boolean published = autoSite.getPublished();
			
			news.setUrl(url);
			news.setTitle(title);
			news.setDescription(description);
			news.setContent(description);
			news.setPublished(published);
			news.setAutoSite(autoSite.getUrl());
			news.setCategory(autoSite.getCategory());
			news.setCreator(autoSite.getCreator());
			news.setAlias();
			news.setId();
			
		} catch (IOException e) {
			news = null;
			e.printStackTrace();
		}
		
		return news;
	}
}
